<?php
namespace App\WebSocket;
use EasySwoole\Socket\AbstractInterface\Controller;

class PushContent extends Controller
{
  //发送推送消息
	public static function appPush($deviceToken="",$message="",$status=0){
      //数据处理，筛选有用的数据字段
      
      
      //证书对应秘钥
      $passphrase = 'Zxcvbnm123';
      // Put your alert message here:
      $ctx = stream_context_create();
      stream_context_set_option($ctx, 'ssl', 'local_cert', 'Temp/push_production.pem');
      stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
      //这个为正是的发布地址
       //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
      $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
      if (!$fp){
      	return false;
      }
      $body['aps'] = array('alert' => array('body'=>'你有一条新消息',"loc-key"=>"new_message"),"badge"=>1,"sound"=>"default");
      // Create the payload body
      /*if($status == 1){
      	$body['aps'] = array('alert' => array('body'=>'你有一条新消息',"loc-key"=>"new_message"),"badge"=>1,"sound"=>"default");
      }else{
      	$body['aps'] = array('content-available' => '1','data'=>$message);
      }
      */
      // Encode the payload as JSON
      $payload = json_encode($body);
      // Build the binary notification
      $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
	  // Send it to the server
      $result = fwrite($fp, $msg, strlen($msg));
      if (!$result){
         fclose($fp);
       return false;
      }else{
         fclose($fp);
        return true;
      }
      // Close the connection to the server
     
    }
  
  
  
  
}
?>